const json = {
    id: 1,
    children: [
        { id: 2, children: [{ id: 3, children: [] }] },
        {
            id: 4,
            children: [
                { id: 5, children: [{
                    id: 8, children: []
                    }] },
                { id: 6, children: [] },
            ],
        },
        { id: 7, children: [] },
    ],
};

//? 每个节点id唯一，编写findNode(id)，返回路径，如findNode(5) 输出 1->4->5
function getChildrenRoutes(json) {

    function recur(json, route,target) {
        const {id,children} = json
        route += id
        if (id === target) {
            res = route
            return
        }
        if (children) {
            children.forEach(item => {
                recur(item, route + '->',target)
            })
        }
    }
    let res = ""
    recur(json,'',8)
    return res
}

console.log(getChildrenRoutes(json));
